package com.jk.customer.controller;

import com.jk.customer.entity.*;
import com.jk.customer.service.SellerService;
import com.jk.customer.utils.OSSUtil;
import com.jk.customer.utils.PoiUtil;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

/**
 * 方法实现说明    (controller)
 * @author      潘天赐
 * @return
 * @exception
 * @date        2021/10/11 20:32
 */
@Controller
public class SellerController {

    @Autowired
    private SellerService sellerService;
    /**
     * 方法实现说明    (查询对应材料单)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/11 20:35
     */
    @RequestMapping("queryMaterials")
    @ResponseBody
    public List<MaterialsEntity> queryMaterials(HttpSession session){
        String showPriceId = (String) session.getAttribute("showPriceId");
        List<MaterialsEntity> list = sellerService.queryMaterials(showPriceId);
        return list;
    }
    /**
     * 方法实现说明    (oss上传)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/12 11:05
     */
    @RequestMapping("upload")
    @ResponseBody
    public Object upload2oss(MultipartFile file) throws Exception{
        String upload2oss = OSSUtil.upload2oss(file);
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("aa", OSSUtil.upload2oss(file));
        return map;
    }
    /**
     * 方法实现说明    (新增报价单)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/12 16:39
     */
    @RequestMapping("addOffer")
    @ResponseBody
    public void addOffer(OfferFormEntity offer, HttpSession session){

        sellerService.addOffer(offer,session);
    }
    /**
     * 方法实现说明    (查看报价)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/12 19:40
     */
    @RequestMapping("queryMaterialsOffer")
    @ResponseBody
    public List<MaterialsEntity> queryMaterialsOffer(HttpSession session){
        String id = (String) session.getAttribute("queryOfferId");
        List<MaterialsEntity> list = sellerService.queryMaterialsOffer(id,session);
        return list;
    }
    /**
     * 方法实现说明    (查看报价)  修改页面
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/19 16:16
     */
    @RequestMapping("queryMaterialsOfferTo")
    @ResponseBody
    public List<MaterialsEntity> queryMaterialsOfferTo(HttpSession session){
        String id = (String) session.getAttribute("updateOfferId");
        List<MaterialsEntity> list = sellerService.queryMaterialsOffer(id, session);
        return list;
    }
    /**
     * 方法实现说明    (回显报价单)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/12 20:56
     */
    @RequestMapping("findByOffer")
    @ResponseBody
    public OfferFormEntity findByOffer(Integer oid,HttpSession session){
        OfferFormEntity offer = sellerService.findByOffer(oid,session);
        return offer;
    }
    /**
     * 方法实现说明    (修改报价)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/13 10:46
     */
    @RequestMapping("updateOffer")
    @ResponseBody
    public void updateOffer(OfferFormEntity offer){
        sellerService.updateOffer(offer);
    }
    /**
     * 方法实现说明    (查询全部材料)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/13 15:26
     */
    @RequestMapping("queryMaterialsAll")
    @ResponseBody
    public List<ProductEntity> queryMaterialsAll(ProductEntity product,HttpSession session){
        List<ProductEntity> list = sellerService.queryMaterialsAll(product,session);
        return list;
    }
    /**
     * 方法实现说明    (查看我的材料单)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/19 16:11
     */
    @RequestMapping("queryMyMaterials")
    @ResponseBody
    public List<MaterialsEntity> queryMyMaterials(HttpSession session){
        String id = (String) session.getAttribute("queryMyMaterialsId");
        List<MaterialsEntity> list = sellerService.queryMaterialsMessage(id);
        return list;
    }
    /**
     * 方法实现说明    (修改材料页面  回显查询)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/20 16:44
     */
    @RequestMapping("queryMyMaterialsTo")
    @ResponseBody
    public List<MaterialsEntity> queryMyMaterialsTo(HttpSession session){
        String id = (String) session.getAttribute("updateMyMaterialsId");
        List<MaterialsEntity> list = sellerService.queryMaterialsMessage(id);
        return list;
    }

    /**
     * 方法实现说明    (跳转查看材料单)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/14 9:09
     */
    @RequestMapping("queryMaterialsShow")
    public String queryMaterialsShow(String id,String name, HttpSession session){
        session.setAttribute("queryMyMaterialsId",id);
        session.setAttribute("queryMyMaterialsName",name);
        return "queryMaterialsShow";
    }
    /**
     * 方法实现说明    (跳转编辑材料单)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/14 9:10
     */
    @RequestMapping("redactMaterials")
    public String redactMaterials(String id,String name, HttpSession session){
        session.setAttribute("updateMyMaterialsId",id);
        session.setAttribute("updateMyMaterialsName",name);
        return "redactMaterials";
    }

    /**
     * 方法实现说明    (修改三表)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/14 19:20
     */
    @RequestMapping("updateMaterials")
    @ResponseBody
    public void updateMaterials(MaterialsEntity materials){
        sellerService.updateMaterials(materials);
    }

    /**
     * 方法实现说明    (下载材料单)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/15 14:26
     */
    @RequestMapping("exportData")
    public void exportData(HttpServletResponse response,Integer id,HttpSession session){
        XSSFWorkbook workbook = new XSSFWorkbook();
        String sheetName ="资源信息";
        String[] titles={"材料单部件:"};
        XSSFSheet sheet = PoiUtil.exportData(workbook, sheetName, titles);
        //MaterialsEntity test = new MaterialsEntity();
        MaterialsEntity list = sellerService.export(id,session); //查询数据
            XSSFRow rowa = sheet.createRow(2);
            rowa.createCell(0).setCellValue("类目");
            rowa.createCell(1).setCellValue("材料名称");
            rowa.createCell(2).setCellValue("材料编码");
            rowa.createCell(3).setCellValue("材料数量");
            XSSFRow row = sheet.createRow(3);
            row.createCell(0).setCellValue(list.getMaterialsType());
            row.createCell(1).setCellValue(list.getMaterialsName());
            row.createCell(2).setCellValue(list.getMaterialsId());
            row.createCell(3).setCellValue(list.getMaterialsCount());

        PoiUtil.down(response, workbook, "资源导出");
    }
    /**
     * 方法实现说明    (下载报价单)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/15 16:16
     */
    @RequestMapping("exportDataOffer")
    public void exportDataOffer(HttpServletResponse response,Integer id,HttpSession session){
        XSSFWorkbook workbook = new XSSFWorkbook();
        String sheetName ="报价单信息";
        String[] titles={"材料单部件:"};
        XSSFSheet sheet = PoiUtil.exportData(workbook, sheetName, titles);
        //MaterialsEntity test = new MaterialsEntity();
        MaterialsEntity list = sellerService.export(id, session); //查询数据
            XSSFRow rowa = sheet.createRow(1);
            rowa.createCell(0).setCellValue("类目");
            rowa.createCell(1).setCellValue("材料名称");
            rowa.createCell(2).setCellValue("材料编码");
            rowa.createCell(3).setCellValue("材料数量");
            rowa.createCell(4).setCellValue("品牌");
            rowa.createCell(5).setCellValue("单价 (元)");
            XSSFRow row = sheet.createRow(2);
            row.createCell(0).setCellValue(list.getMaterialsType());
            row.createCell(1).setCellValue(list.getMaterialsName());
            row.createCell(2).setCellValue(list.getMaterialsId());
            row.createCell(3).setCellValue(list.getMaterialsCount());
            row.createCell(4).setCellValue(list.getBrand());
            row.createCell(5).setCellValue(list.getPrice());
            XSSFRow row2 = sheet.createRow(4);//循环list结束后的下一行
            row2.createCell(0).setCellValue("服务项:");
            XSSFRow row3 = sheet.createRow(5);//循环list结束后的下一行
            row3.createCell(0).setCellValue("运费:");
            row3.createCell(1).setCellValue(list.getFreight());
            XSSFRow row4 = sheet.createRow(6);//循环list结束后的下一行
            row4.createCell(0).setCellValue("组装费:");
            row4.createCell(1).setCellValue(list.getAssemblingCharge());
            XSSFRow row5 = sheet.createRow(7);//循环list结束后的下一行
            row5.createCell(0).setCellValue("其他:"+list.getOtherBusiness());
            row5.createCell(1).setCellValue(list.getBusinessPrice());
            XSSFRow row6 = sheet.createRow(8);//循环list结束后的下一行
            row6.createCell(0).setCellValue("备注:");
            row6.createCell(1).setCellValue(list.getOfferFormRemark());
            XSSFRow row7 = sheet.createRow(9);//循环list结束后的下一行
            row7.createCell(0).setCellValue("附件:");
            row7.createCell(1).setCellValue(list.getOfferFormAccessory());
            XSSFRow row8 = sheet.createRow(10);//循环list结束后的下一行
            row8.createCell(0).setCellValue("供货周期(天):");
            row8.createCell(1).setCellValue(list.getDeliveryCycle());
            XSSFRow row9 = sheet.createRow(13);//循环list结束后的下一行
            row9.createCell(9).setCellValue("报价有效时间(天):");
            row9.createCell(12).setCellValue("项目总价:");
            XSSFRow row10 = sheet.createRow(14);//循环list结束后的下一行
            row10.createCell(10).setCellValue(list.getOfferFormValidTime());
            row10.createCell(12).setCellValue(list.getTotalPrices());

        PoiUtil.down(response, workbook, "资源导出");
    }
    /**
     * 方法实现说明    (查询上传文件)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/18 17:14
     */
    @RequestMapping("queryPictureFile")
    @ResponseBody
    public List<PictureFile> queryPictureFile(String id,HttpSession session){
        List<PictureFile> list = sellerService.queryPictureFile(id,session);
        return list;
    }
    /**
     * 方法实现说明    (上传文件)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/19 17:21
     */
    @RequestMapping("uploadFile")
    @ResponseBody
    public Object uploadFile(MultipartFile file) throws IOException {
        String url = OSSUtil.getUrl(file);
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("aa",url);
        return map;
        /*String upload2oss = OSSUtil.upload2oss(file);
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("aa", OSSUtil.upload2oss(file));
        return map;*/
    }
    /**
     * 方法实现说明    (添加文件)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/18 19:07
     */
    @RequestMapping("addFile")
    @ResponseBody
    public void addFile(PictureFile file,HttpSession session){
        sellerService.addFile(file,session);
    }
    /**
     * 方法实现说明    (删除文件)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/18 20:17
     */
    @RequestMapping("deleteFile")
    @ResponseBody
    public void deleteFile(Integer id){
        sellerService.deleteFile(id);
    }
    /**
     * 方法实现说明    (跳转填写材料页面)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/19 20:26
     */
    @RequestMapping("JumpToMaterials")
    public String JumpToMaterials(String queryPictureId,String commonQueryId, Model model){
        model.addAttribute("queryPictureId",queryPictureId);
        model.addAttribute("commonQueryId",commonQueryId);
        return "fillInMaterials";
    }
    /**
     * 方法实现说明    (动态下拉框  类目)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/19 20:31
     */
    @RequestMapping("queryMaterialsType")
    @ResponseBody
    public List<MaterialsTypeEntity> queryMaterialsType(){
        List<MaterialsTypeEntity> list = sellerService.queryMaterialsType();
        return list;
    }
    /**
     * 方法实现说明    (新增寻图材料)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/20 9:03
     */
    @RequestMapping("addPictureMaterials")
    @ResponseBody
    public void addPictureMaterials(MaterialsEntity materials, HttpSession session){
        sellerService.addPictureMaterials(materials,session);
    }
    /**
     * 方法实现说明    (跳转修改材料页面)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/20 14:15
     */
    @RequestMapping("updateMaterialsPtc")
    public String updateMaterialsPtc(String queryPictureId,String commonQueryId, Model model){
        model.addAttribute("queryPictureId",queryPictureId);
        model.addAttribute("commonQueryId",commonQueryId);
        return "updateMaterialsPtc";
    }
    /**
     * 方法实现说明    (修改回显材料页面)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/20 15:00
     */
    @RequestMapping("queryPictureMaterials")
    @ResponseBody
    public MaterialsEntity queryPictureMaterials(String id){
        MaterialsEntity list = sellerService.queryPictureMaterials(id);
        return list;
    }
    /**
     * 方法实现说明    (修改材料信息)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/20 15:01
     */
    @RequestMapping("updatePictureMaterials")
    @ResponseBody
    public void updatePictureMaterials(MaterialsEntity materials){
        sellerService.updatePictureMaterials(materials);
    }
    /**
     * 方法实现说明    (回显订单信息)
     * @author      潘天赐
     * @return
     * @exception
     * @date        2021/10/21 16:38
     */
    @RequestMapping("findByOrder")
    @ResponseBody
    public BusinessClass findByOrder(String id,HttpSession session) throws ParseException {
        BusinessClass byOrder = sellerService.findByOrder(id, session);
        return byOrder;
    }

}
